package com.wenhai.spring.bean.definition;

import com.wenhai.spring.bean.factory.DefaultUserFactory;
import com.wenhai.spring.bean.factory.UserFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @Author:wenhai
 * @Description: 外部单体对象注册
 * @Date:2020/3/7
 */
public class SingletonBeanRegistrationDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext();
        UserFactory defaultUserFactory = new DefaultUserFactory();
        ConfigurableListableBeanFactory beanFactory = configApplicationContext.getBeanFactory();
        beanFactory.registerSingleton("userFactory", defaultUserFactory);
        configApplicationContext.refresh();
        UserFactory userFactory = configApplicationContext.getBean("userFactory", UserFactory.class);
        System.out.println(userFactory == defaultUserFactory);
        configApplicationContext.close();
    }
}
